﻿using System;
using System.Collections.Generic;
using System.Linq;
using LinQProject;
using ServiceProject.EntityService;
using Infrastructure.Coding;

namespace ServiceProject
{
    public class OrderBussiness
    {
        private static OrderService ordService { get; set; }
        private static OrderDetailService ordDetailService { get; set; }
        //private static ProductDetailService prodDetailService { get; set; }
        private static ProductService productService { get; set; }
        public OrderBussiness(OrderService ordServ,OrderDetailService ordDetailServ, ProductService prodServ)
        {
            ordService = ordServ;
            ordDetailService = ordDetailServ;
            //prodDetailService = prodDetailServ;
            productService = prodServ;
        }

        public void SaveOrder(Order order, IList<OrderDetail> listOrderDetail)
        {
            int orderId = (int) ordService.InsertOrder(order);
            ordDetailService.InsertOrderDetailsForOrder(listOrderDetail,orderId);
        }

        //public Product GetLessOrderedProduct(DateTime dtInit, DateTime dtFinal)
        //{
        //    IList<Product> listaProducts;
        //    IList<Order> listaOrder=ordService.GetOrdersInInterval(dtInit,dtFinal);
        //    if (listaOrder == null || listaOrder.Count == 0)
        //        listaProducts = productService.GetAllProducts();
        //    else
        //    {
        //        IList<ProductDetail> listaProductDetail = ordDetailService.GetProductDetails(listaOrder);
        //        if (listaProductDetail == null || listaProductDetail.Count == 0)
        //            listaProducts = productService.GetAllProducts();
        //        else
        //            listaProducts = prodDetailService.GetProducts(listaProductDetail);
        //    }
        //    return new Generator().RandomlySelectItem(listaProducts);
        //}

        //public Product GetMostOrderedProduct()
        //{
        //    IList<OrderDetail> listaorderDetails = ordDetailService.GetAllOrderDetails();
        //    var products = from od in listaorderDetails
        //                   group od by od.ProductDetail.Product
        //                   into g
        //                       select new {prod = g.Key, cant =g.Sum(od=>od.ProductCount)};
        //    int maximCount = 0;
        //    Product productToReturn = null;
        //    foreach (var product in products)
        //    {
        //        if (maximCount<product.cant)
        //        {
        //            maximCount = product.cant;
        //            productToReturn = product.prod;
        //        }
        //    }
        //    return productToReturn;
        //}

        public IList<OrderDetail> GetOrderDetailsForOrder(int orderId)
        {
            return ordDetailService.GetOrderDetails(orderId);
        }
    }
}
